<link rel="stylesheet" type="text/css" href="../stylesheet/style.css"/>
<?php
/*
* @author Nguyen Xuan Hong
* @copyright 2011
*/

require_once('../includes/album.tbl.php');
require_once('../includes/branch.tbl.php');
require_once('../includes/picture.tbl.php');

$branch_id = $_GET['branch'];
	
if($_GET['mode'] == 'listalbum')
{
	$pClsAlbum = new clsAlbumTable();	
	if($pClsAlbum->avaibleSubmit()){
		$pClsAlbum->loadBuffer();
		$pClsAlbum->setvalue('branch_id', $branch_id);
		
		$pClsAlbum->insertDB();
		$id = $pClsAlbum->value('id');
   		
		$image = upload_file("Upload/", 'imgbrowser_image', $pClsAlbum->value('image'), "alb".$id);
		if($image != "")
		{
			$pClsAlbum->setvalue('image', $image);
		}
		
		$pClsAlbum->updateDB('id',$id);
		
		echo acpredirect();
		exit();
	}
	echo $pClsAlbum->createForm('Thêm album ảnh');
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstalbum',array(array("ID", "1"),array("Tên album", 0),array("Sửa", 1),array("Xóa", 1)));
	$list_album = $pClsAlbum->listobj($branch_id);
	foreach($list_album as $album){
		$html_remove =      '<a href="?page=album&mode=deletealbum&branch='.$branch_id.'&id='.$album->value('id').'">Xóa</a>';
		$html_edit = 	'<a href="?page=album&mode=editalbum&branch='.$branch_id.'&id='.$album->value('id').'">Sửa</a>';	
		$table->addRow($album->value('id'),array($album->value('id'),$album->value('title'),$html_edit,$html_remove));
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'editalbum')
{
	$edit = (int)sget('id');
	$pClsAlbum = new clsAlbumTable($edit);
	if($pClsAlbum->avaibleSubmit()){
		$pClsAlbum->loadBuffer();
		
		$image = upload_file("Upload/", 'imgbrowser_image', $pClsAlbum->value('image'), "alb".$id);
		if($image != "")
		{
			$pClsAlbum->setvalue('image', $image);
		}
		
		$pClsAlbum->updateDB('id',$edit);
		echo acpredirect('page=album&mode=listalbum&branch='.$branch_id);
		exit();
	}
	$pClsAlbum->loadDB('id',$edit);
	echo $pClsAlbum->createForm('Sửa Album');
	
	$pClsPicture = new clsPictureTable();
	require_once('../includes/templates.php');
	$table = new clsTable('lstpicture',array(array("ID", "1"),array("Tiêu đề ảnh", 0),array("Ảnh", 0),array("Sửa", 1),array("Xóa", 1)));
	$list_picture = $pClsPicture->listobj($edit);
	foreach($list_picture as $picture){
		$html_image =      '<img class="ui-state-default ui-corner-all" src="resize.php?url='.$picture->value('url').'&amp;t=ratio&amp;width=100&amp;height=50">';
		$html_remove =      '<a href="?page=album&mode=deletepicture&branch='.$branch_id.'&album='.$edit.'&id='.$picture->value('id').'">Xóa</a>';
		$html_edit = 	'<a href="?page=album&mode=editpicture&branch='.$branch_id.'&album='.$edit.'&id='.$picture->value('id').'">Sửa</a>';	
		$table->addRow($picture->value('id'),array($picture->value('id'),$picture->value('title'),$html_image, $html_edit,$html_remove));
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deletealbum'){		
	// Redefine insert
	$deleteid = sget('id');			
	$pClsAlbum = new clsAlbumTable($deleteid);
	if(!$_POST['submit']){
		?>
        
	<center><div class="notification warning">
        <span></span>
        <p><strong>Warning!</strong> 
        	<div class="text">Bạn có muốn xóa Album này không</div> 
            <div class="btn"><form method='post'><input type='submit' name='submit' value = 'Xóa' /></form></div>
         </p>
	</div></center>
        <?
		exit();
	}
	$pClsAlbum->removeDB('id', $deleteid);
       header("location:index.php?page=album&mode=listalbum&branch=".$branch_id);
	exit();
}

if($_GET['mode'] == 'editpicture')
{
	$edit = (int)sget('id');
	$album_id = (int)sget('album');
	 $pClsPicture = new clsPictureTable($edit);
	$pClsAlbum = new clsAlbumTable();	
       $pClsPicture->attList['album_id']['list'] = $pClsAlbum->cbolist($branch_id);
       
	if($pClsPicture->avaibleSubmit()){
		$pClsPicture->loadBuffer();
        $image = upload_file("Upload/", 'imgbrowser_url', $pClsPicture->value('image'), "pic".$edit);
		if($image != "")
		{
			$pClsPicture->setvalue('url', $image);
		} 
		
		$pClsPicture->updateDB('id',$edit);
        echo acpredirect('page=album&mode=editalbum&branch='.$branch_id.'&id='.$album_id);
		exit();
	}
	$pClsPicture->loadDB('id',$edit);
    echo $pClsPicture->createForm('Sủa thông tin ảnh');
	exit();
}

if($_GET['mode'] == 'deletepicture'){		
  	// Redefine insert
  	$deleteid = sget('id');
	$album_id = (int)sget('album');			
  	$pClsPicture = new clsPictureTable($deleteid);
  	if(!$_POST['submit']){
  ?>
	
	<center><div class="notification warning">
	<span></span>
	<p><strong>Warning!</strong> 
	<div class="text">Bạn có muốn xóa Ảnh này không</div> 
	<div class="btn"><form method='post'><input type='submit' name='submit' value = 'Xóa' /></form></div>
	</p>
	</div></center>
    <?
    exit();
   }
   $pClsPicture->removeDB('id', $deleteid);
   echo acpredirect("page=album&mode=editalbum&branch=".$branch_id.'&id='.$album_id);
   exit();
  }
	
if($_GET['mode'] == 'addpicture')
{
	$pClsAlbum = new clsAlbumTable();	
    $pClsPicture = new clsPictureTable();
    $pClsPicture->attList['album_id']['list'] = $pClsAlbum->cbolist($branch_id);
	if($pClsPicture->avaibleSubmit())
	{
   		$pClsPicture->loadBuffer();
		$pClsPicture->insertDB();
		$id = $pClsPicture->value('id');
   		
		$url = upload_file("Upload/", 'imgbrowser_url', $pClsPicture->value('url'), "pic".$id);
		if($url != "")
		{
        	$pClsPicture->setvalue('url', $url);
		}
		$pClsPicture->updateDB('id',$id);
			
   		echo acpredirect();
   		exit();
	}
    echo $pClsPicture->createForm('Thêm hình ảnh vào album');
	exit();
}	
  
?> 














  